import Dep from "./dep.js";
import observe from "./observe.js";
//   defineReactive(obj, key, obj[key])
export default function defineReactive(target, key, val) {
    // 递归每一个value，处理value为object的情况
   const childOb= observe(val)
   const dep = new Dep()
    Object.defineProperty(target, key, {
        get() {

            if(Dep.target){
                dep.depend()
                if(childOb){
                    childOb.dep.depend()
                }
            }
            console.log('getter  key = ' + key);
            return val
        },
        set(newV) {
            if (newV === val) return
            console.log(`setter: ${key} = ${newV}`)
            val = newV
            // 设置新的值，并对新的值重新设置响应式
            observe(val)
            dep.notify()
        }
    })
}